package com.yyb.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yyb.common.PageInfo;
import com.yyb.domain.SysDictData;
import com.yyb.domain.SysDictType;
import com.yyb.dto.SysDictTypeAddDTO;
import com.yyb.dto.SysDictTypeQueryDTO;
import com.yyb.dto.SysDictTypeUpdateDTO;
import com.yyb.exception.BusinessException;
import com.yyb.mapper.SysDictDataMapper;
import com.yyb.mapper.SysDictTypeMapper;
import com.yyb.service.SysDictTypeService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.lang.annotation.Target;
import java.util.List;

/**
 * 
 * 
 * 
 */
@Service
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictType> implements SysDictTypeService {

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 分页查询字典类型数据
     *
     * @param pageInfo
     * @return
     */
    @Override
    public PageInfo<SysDictType, SysDictTypeQueryDTO> getByPage(PageInfo<SysDictType, SysDictTypeQueryDTO> pageInfo) {
        Page<SysDictType> startPage = this.startPage(pageInfo.getPageNumber(), pageInfo.getPageSize());
        List<SysDictType> dictTypes = this.sysDictTypeMapper.queryAllDictType(pageInfo.getParams());
        //向pageInfo 里面放数据
        pageInfo.setTotalCount(startPage.getTotal());
        pageInfo.setTotalPage(startPage.getPages());
        pageInfo.setList(dictTypes);
        return pageInfo;
    }

    /**
     * 添加字典类型
     *
     * @param sysDictTypeAddDTO
     * @return
     */
    @Override
    public int insert(SysDictTypeAddDTO sysDictTypeAddDTO) {
        SysDictType sysDictType=new SysDictType();
        //把dto里面的数据copy到sysDictType
        BeanUtils.copyProperties(sysDictTypeAddDTO,sysDictType);
        //填充其它数据
        sysDictType.setCreateBy(this.getUsername());
        DateTime date = DateUtil.date();
        sysDictType.setCreateTime(date);
        sysDictType.setUpdateTime(DateUtil.date());
        //保存
        return this.sysDictTypeMapper.insert(sysDictType);
    }

    /**
     * 根据ID查询字典类型
     *
     * @param dictId
     * @return
     */
    @Override
    public SysDictType selectByPrimaryKey(Long dictId) {
        return this.sysDictTypeMapper.selectByPrimaryKey(dictId);
    }

    /**
     * 修改字典类型
     *
     * @param sysDictTypeUpdateDTO
     * @return
     */
    @Override
    public int updateByPrimaryKey(SysDictTypeUpdateDTO sysDictTypeUpdateDTO) {
        SysDictType sysDictType=new SysDictType();
        //把dto里面的数据copy到sysDictType
        BeanUtils.copyProperties(sysDictTypeUpdateDTO,sysDictType);
        //填充其它数据
        DateTime date = DateUtil.date();
        sysDictType.setUpdateBy(this.getUsername());
        sysDictType.setUpdateTime(DateUtil.date());
        //保存
        return this.sysDictTypeMapper.updateByPrimaryKeySelective(sysDictType);
    }

    /**
     * 删除字典类型
     * 删除sys_dict_type里面的数据
     * 删除sys_dict_data里面的数据
     *
     * @param dictId
     * @return
     */
    @Override
    @Transactional
    public int deleteByPrimaryKey(Long dictId) {
        SysDictType sysDictType = this.sysDictTypeMapper.selectByPrimaryKey(dictId);
        if(ObjectUtils.isEmpty(sysDictType)){
            throw new BusinessException("字典类型不存在");//交给全局异常处理处理
        }
        int row=this.sysDictTypeMapper.deleteByPrimaryKey(sysDictType.getDictId());
        //删除sys_dict_data里面的数据
        String dictType = sysDictType.getDictType();
        this.sysDictDataMapper.deleteByDictType(dictType);
        return row;
    }


}
